//
//  CZHomeTableViewCell.swift
//  WeiBo
//
//  Created by Apple on 16/9/6.
//  Copyright © 2016年 KY. All rights reserved.
//

import UIKit

/*
    首页cell分为三大块视图开发
        1. 原创微博视图
        2. 转发微博视图
        3. 底部tool视图
 */

//  首页内容间距
let HomeCellMargin: CGFloat = 10

//  自定义首页cell
class CZHomeTableViewCell: UITableViewCell {
    //  toolbar顶部约束
    var toolBarTopConstraint: Constraint?
    //  viewModel模型数据
    var statusViewModel: CZStatusViewModel? {
        didSet {
            //  绑定处理的微博数据
            originalView.statusViewModel = statusViewModel
            //  卸载上次约束
            toolBarTopConstraint?.uninstall()
            //  判断是否有转发微博内容
            if statusViewModel?.status?.retweeted_status != nil {
                //  有转发微博内容
                //  绑定模型数据，更新约束，显示转发微博视图
                retweetView.statusViewModel = statusViewModel
                retweetView.isHidden = false
                toolBar.snp_updateConstraints(closure: { (make) in
                    toolBarTopConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
                })
                
            } else {
                //  没有转发微博内容
                //  更新约束，隐藏转发微博视图
                retweetView.isHidden = true
                toolBar.snp_updateConstraints(closure: { (make) in
                    toolBarTopConstraint = make.top.equalTo(originalView.snp_bottom).constraint
                })
            }
            
            
            
            toolBar.statusViewModel = statusViewModel
            
            
            
        }
    }
    
    // MARK:    --懒加载控件
    //  原创微博视图
    private lazy var originalView: CZStatusOriginalView = {
        let view = CZStatusOriginalView()
        return view
    }()
    //  转发微博视图
    private lazy var retweetView: CZStatusRetweetView = {
        let view = CZStatusRetweetView()
        return view
    }()
    
    //  底部toolbar
    private lazy var toolBar: CZStatusToolBar = CZStatusToolBar()
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //  添加控件设置约束
    private func setupUI() {
        //  设置背景色
        contentView.backgroundColor = UIColor(white: 0.95, alpha: 1)
        contentView.addSubview(originalView)
        contentView.addSubview(retweetView)
        contentView.addSubview(toolBar)
        //  设置约束
        originalView.snp_makeConstraints { (make) in
            make.leading.equalTo(contentView)
            make.top.equalTo(contentView).offset(8)
            make.trailing.equalTo(contentView)
            
        }
        
        retweetView.snp_makeConstraints { (make) in
            make.top.equalTo(originalView.snp_bottom)
            make.leading.equalTo(originalView)
            make.trailing.equalTo(originalView)
            
        }
        
        //  toolBar约束等于cell的contentView的底部
        toolBar.snp_makeConstraints { (make) in
            make.leading.equalTo(retweetView)
            toolBarTopConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
            make.trailing.equalTo(retweetView)
            make.bottom.equalTo(contentView)
        }
    
        //  关键约束设置contentView的底部约束
//        contentView.snp_makeConstraints { (make) in
//            make.bottom.equalTo(originalView)
//            make.leading.equalTo(self)
//            make.trailing.equalTo(self)
//            make.top.equalTo(self)
//        }
    }
    
    
    

}
